package io.yizoo.business.controller;

import io.yizoo.business.entity.*;
import io.elm.core.CoreController;
import io.yizoo.system.entity.sys_org;
import io.yizoo.system.entity.sys_user;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.EnumSet;
import java.util.List;

@RestController
@RequestMapping("/business/analysis/cockpit/month")
public class analysis_cockpit_monthController  extends CoreController {

    @PostMapping("/SelectDdTotalData")
    public Object SelectDdTotalData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cmis_order.class)
                        .Select("SUM(total_amount) AS total_amount,MONTH(order_date) AS month")
                        .IsWhere("and YEAR(order_date) = #{now_year}", "now_year")
                        .Where("group by MONTH(order_date)")
                        .OrderBy("MONTH(order_date)")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/SelectHtTotalData")
    public Object SelectHtTotalData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cmis_contracts.class)
                        .Select("SUM(amount) AS total_amount,MONTH(contract_date) AS month")
                        .IsWhere("and YEAR(contract_date) = #{now_year}", "now_year")
                        .Where("group by MONTH(contract_date)")
                        .OrderBy("MONTH(contract_date)")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/SelectPjTotalData")
    public Object SelectPjTotalData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj.class, "p")
                        .Select("SUM(money) AS total_amount,MONTH(p.b_sdate) AS month")
                        .LeftJoin(biz_cmis_order.class, "o", "p.order_key = o.sys_code_key AND YEAR(o.order_date) = YEAR(p.b_sdate)")
                        .LeftJoin(biz_cmis_contracts.class, "c", "p.contract_key = c.sys_code_key AND YEAR(c.contract_date) = YEAR(p.b_sdate)")
                        .IsWhere("and YEAR(p.b_sdate) = #{now_year}", "now_year")
                        .Where("and o.id IS NULL AND c.id IS NULL")
                        .Where("group by MONTH(p.b_sdate)")
                        .OrderBy("MONTH(p.b_sdate)")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/SelectPjCostSum")
    public Object SelectPjCostSum(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_bms_reim.class)
                        .Select("SUM( col_3 )/10000 AS total_cost,MONTH(sys_createtime) AS month")
                        .IsWhere("and YEAR ( sys_createtime ) = #{now_year}", "now_year")
                        .Where("group by MONTH(sys_createtime)")
                        .OrderBy("MONTH(sys_createtime)")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/SelectOutsourceCostSum")
    public Object SelectOutsourceCostSum(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cmis_contracts_zc.class)
                        .Select("SUM( amount ) AS total_cost,MONTH(contract_date) AS month")
                        .IsWhere("and YEAR ( contract_date ) = #{now_year}", "now_year")
                        .Where("group by MONTH(contract_date)")
                        .OrderBy("MONTH(contract_date)")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/SelectLaborCostSum")
    public Object SelectLaborCostSum(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_hrm_salary.class)
                        .Select("SUM( cope_salary )/10000 AS total_cost,CONVERT(SUBSTRING(month,5,2), SIGNED) AS month")
                        .IsWhere("and SUBSTRING(month,1,4) = #{now_year}", "now_year")
                        .Where("group by SUBSTRING(month,5,2)")
                        .OrderBy("SUBSTRING(month,5,2)")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/SelectLaborCost")
    public Object SelectLaborCost(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_hrm_salary.class,"t")
                        .Select("SUM( t.cope_salary )/10000 AS total_cost,CONVERT(SUBSTRING(t.month,5,2), SIGNED) AS month,c.cost_type")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code = t.emp_code")
                        .LeftJoin(biz_hrm_ei_rel_org.class,"b","b.ei_id = a.id and b.pm = 1")
                        .LeftJoin(sys_org.class,"c","c.id = b.org_id")
                        .IsWhere("and SUBSTRING(t.month,1,4) = #{now_year}", "now_year")
                        .Where("group by SUBSTRING(t.month,5,2),c.cost_type")
                        .OrderBy("SUBSTRING(t.month,5,2)")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/SelectPjCost")
    public Object SelectPjCost(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_bms_reim.class,"t")
                        .Select("SUM( t.col_3 )/10000 AS total_cost,MONTH(t.sys_createtime) AS month,c.cost_type")
                        .LeftJoin(sys_user.class,"d","d.id = t.sys_createuser")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code = d.emp_code")
                        .LeftJoin(biz_hrm_ei_rel_org.class,"b","b.ei_id = a.id and b.pm = 1")
                        .LeftJoin(sys_org.class,"c","c.id = b.org_id")
                        .IsWhere("and YEAR ( t.sys_createtime ) = #{now_year}", "now_year")
                        .Where("group by MONTH(t.sys_createtime),c.cost_type")
                        .OrderBy("MONTH(t.sys_createtime)")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/SelectOutsourceCost")
    public Object SelectOutsourceCost(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cmis_contracts_zc.class,"t")
                        .Select("SUM( t.amount ) AS total_cost,MONTH(t.contract_date) AS month,c.cost_type")
                        .LeftJoin(biz_hrm_ei_basic.class, "a", "a.emp_code = t.charge_person")
                        .LeftJoin(biz_hrm_ei_rel_org.class, "b", "b.ei_id = a.id and b.pm = 1")
                        .LeftJoin(sys_org.class, "c", "c.id = b.org_id")
                        .IsWhere("and YEAR ( t.contract_date ) = #{now_year}", "now_year")
                        .Where("group by MONTH(t.contract_date),c.cost_type")
                        .OrderBy("MONTH(t.contract_date)")
                        .GetFactor());
        return JsonResult(ret);
    }
}
